/*
package cn.sccl.dirms.topo.discovery.service.impl;

import cn.sccl.dcn.dms.model.DmsSwitchSegment;
import cn.sccl.dcn.dms.service.DmsSwitchSegmentManager;
import cn.sccl.dcn.enumConst.RelationTypeEnum;
import cn.sccl.dcn.enumConst.ResTypeEnum;
import cn.sccl.dcn.instance.model.Ne;
import cn.sccl.dcn.instance.model.Neport;
import cn.sccl.dcn.instance.model.NodeRelations;
import cn.sccl.dcn.instance.model.Nodes;
import cn.sccl.dcn.instance.service.*;
import cn.sccl.dcn.pub.model.Status;
import cn.sccl.dcn.pub.service.StatusManager;
import cn.sccl.dcn.topo.model.Element;
import cn.sccl.dcn.topo.service.ElementManager;
import cn.sccl.common.exception.BusinessException;
import cn.sccl.dirms.device.model.Dev;
import cn.sccl.dirms.device.service.DevManager;
import cn.sccl.dirms.planning.service.VlanManager;
import cn.sccl.dirms.topo.discovery.model.DeviceInfo;
import cn.sccl.dirms.topo.discovery.model.LinkInfo;
import cn.sccl.dirms.topo.discovery.model.NewHostResult;
import cn.sccl.dirms.topo.discovery.model.PortInfo;
import cn.sccl.dirms.topo.discovery.service.DiscoveryDeviceManager;
import cn.sccl.dirms.zabbix.Zabbix;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

@Component
public class DiscoveryDeviceManagerImpl implements DiscoveryDeviceManager {

    @Autowired
    private NeportManager neportManager;

    @Autowired
    private NeManager neManager;

    @Autowired
    private NodesManager nodesManager;

    @Autowired
    private NodeRelationsManager nodeRelationsManager;

    @Autowired
    private StatusManager statusManager;

    @Autowired
    private DeviceManager deviceManager;

    @Autowired
    private ElementManager elementManager;

    @Autowired
    private DmsSwitchSegmentManager switchSegmentManager;

    @Autowired
    private DevManager devManager;
    @Autowired
    private VlanManager vlanManager;
    @Autowired
    private PortManager portManager;


    private String graphId;

    @Override
    public NewHostResult newHost(DeviceInfo deviceinfo, ConcurrentMap concurrentMap) {
        this.graphId = (String) concurrentMap.get("graphId");
        NewHostResult result = new NewHostResult();
        String id = "0";
        String ip = deviceinfo.getDeviceIP();
        Zabbix zabbix = Zabbix.getZabbix();
        List<Map> interfaceList, groupList;
        Map<String, Object> zabbixResult, interfaceMap, params, groupMap;
        params = new HashMap();
        params.put("host", ip);
        //判断主机是否存在
        Map<String, Object> filter = new HashMap<String, Object>();
        List<String> filterContent = new ArrayList<String>();
        filterContent.add(ip);
        filter.put("host", filterContent);
        params.put("filter", filter);
        zabbixResult = zabbix.call("host.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) zabbixResult.get("result");
        if (mapList.size() == 1) {
            id = mapList.get(0).get("hostid").toString();
        } else {
            //不存在创建主机
            params = new HashMap();
            params.put("host", ip);
            interfaceList = new ArrayList<Map>();
            interfaceMap = new HashMap();
            interfaceMap.put("type", 2);//交换机
            interfaceMap.put("main", 1);
            interfaceMap.put("useip", 1);
            interfaceMap.put("ip", ip);
            interfaceMap.put("dns", "");
            interfaceMap.put("port", "161");
            interfaceList.add(interfaceMap);
            params.put("interfaces", interfaceList);
            groupList = new ArrayList<Map>();
            groupMap = new HashMap();
            groupMap.put("groupid", "12");
            groupList.add(groupMap);
            params.put("groups", groupList);
            zabbixResult = zabbix.call("host.create", params);
            Map<String, Object> resultMap = (Map<String, Object>) zabbixResult.get("result");
            if (resultMap != null) {
                id = ((JSONArray) resultMap.get("hostids")).get(0).toString();
            } else {
                id = "0";
            }
        }
        //存储zabbix hostid
        deviceinfo.setZabbixId(id);
        Map findMap = new HashMap();

        //存储新版设备信息
        findMap = new HashMap();
        findMap.put("manIp", deviceinfo.getDeviceIP());
        findMap.put("mac", deviceinfo.getDeviceMAC());
        Dev dev = devManager.findUniqueBy(findMap);
        if(dev==null){
            dev = new Dev();
        }
        dev.setDevName(deviceinfo.getDeviceName());
        dev.setManIp(deviceinfo.getDeviceIP());
        dev.setRCommunity(deviceinfo.getSnmpConfig().getReadCommunity());
        dev.setWCommunity(deviceinfo.getSnmpConfig().getWriteCommunity());
        dev.setMac(deviceinfo.getDeviceMAC());
        dev.setHostId(deviceinfo.getZabbixId());
        dev.setDevType(deviceinfo.getDeviceType());
        dev.setDevFac(deviceinfo.getSystemInfo().getSysContact());
        dev.setDevDesc(deviceinfo.getSystemInfo().getSysDesc());
        devManager.save(dev);
        deviceinfo.setDeviceId(dev.getId());

        //判断资源是否存在
        findMap = new HashMap();
        findMap.put("mark", id);
        Nodes neNode = nodesManager.findUniqueBy(findMap);

        //处理网元节点
        if (neNode == null) {
            neNode = new Nodes();
            neNode.setCode(deviceinfo.getMark());
            neNode.setMark(id);
            neNode.setRstype(Long.valueOf(ResTypeEnum.NE_DMS_FA_SWITCH.getValue()));
            neNode.setInstanceclassname(ResTypeEnum.NE_DMS_FA_SWITCH.getClassName());
            neNode.setName(deviceinfo.getDeviceName() + "【" + ip + "】");
            neNode.setEname(deviceinfo.getDeviceName());
            neNode.setTopolable(deviceinfo.getDeviceName() + "【" + ip + "】");
            nodesManager.save(neNode);
        } else {
            neNode.setName(deviceinfo.getDeviceName() + "【" + ip + "】");
            neNode.setEname(deviceinfo.getDeviceName());
            neNode.setTopolable(deviceinfo.getDeviceName() + "【" + ip + "】");
            nodesManager.save(neNode);
        }
        //存储zabbix hostid
        deviceinfo.setNodeId(neNode.getId());
        //创建网元属性及其网元端口
        Ne ne = neManager.getByNodeId(neNode.getId());
        if (ne == null) {
            //网元状态
            Status neStatus = new Status();
            statusManager.save(neStatus);
            //网元属性
            ne = new Ne();
            ne.setNodeId(neNode.getId());
            ne.setStatusId(neStatus.getId());
            neManager.save(ne);
        }
        //网元端口信息
        List<PortInfo> ifLIst = deviceinfo.getPortInfos();
        for (PortInfo portInfo : ifLIst) {
            //网元端口状态
            Status nePortStatus = new Status();
            statusManager.save(nePortStatus);
            //网元端口属性
            Neport neport = neportManager.findByMark(portInfo.getMark());
            if (neport == null) {
                neport = new Neport();
                String index = String.valueOf(portInfo.port);
                neport.setCode(index);
                neport.setSerial(index);
                neport.setEcode(index);
                neport.setPortType(Long.valueOf(ResTypeEnum.EX_PORT.getValue()));
                neport.setInmsconnecedid(portInfo.getMark());
            }
            neport.setName(portInfo.getName());
            Boolean newNodeFLag = false;
            if (neport.getNodeId() == null || !neNode.getId().equals(neport.getNodeId())) {
                newNodeFLag = true;
                neport.setNodeId(neNode.getId());
            }
            neportManager.save(neport);
            if (newNodeFLag) {
                neport.setNodeId(neNode.getId());
                //网元端口与网元的关系
                NodeRelations nePort2NeR = new NodeRelations();
                nePort2NeR.setChildrenId(neport.getId());
                nePort2NeR.setParentId(neNode.getId());
                nePort2NeR.setRelationType(Long.valueOf(RelationTypeEnum.LOGIC.getValue()));
                nodeRelationsManager.save(nePort2NeR);
            }
            result.getNeportMap().put(this.toMapKey(portInfo), neport);
        }


        //生成物理的框和端子
        deviceManager.saveShelfByDiscover(ne.getId());

        //资源拓扑图
        Element element = elementManager.findByNodeAndGraph(neNode.getId(), graphId);
        if (element == null) {
            element = new Element();
            element.setGraphyId(graphId);
            element.setElementType(1L);
            element.setNodeId(neNode.getId());
            element.setX(Math.random() * 100);
            element.setY(Math.random() * 100);
            element.setEntityType(Long.valueOf(ResTypeEnum.NE_DMS_FA_SWITCH.getValue()));
            elementManager.save(element);
        }
        elementManager.updateElementLayerAndStyle(element.getId());

        result.getNeElementMap().put(this.toMapKey(deviceinfo), element);
        result.setZabbixHostId(id);
        return result;
    }

    public String[] newLink(LinkInfo linkInfo, Map<String, Object> idMap, ConcurrentMap concurrentMap) {
        this.graphId = (String) concurrentMap.get("graphId");

        Nodes linkNodes = nodesManager.findByMark(linkInfo.getMark());
        if (linkNodes == null) {
            linkNodes = nodesManager.findByMark(linkInfo.getReverseMark());
        }

        //已创建线
        Element element = null;
        DmsSwitchSegment switchSegment = null;
        if (linkNodes != null) {
            element = elementManager.findByNodeAndGraph(linkNodes.getId(), graphId);
            switchSegment = switchSegmentManager.getByNodeId(linkNodes.getId());
        } else {
            //线对应的节点数据
            linkNodes = new Nodes();
            element = new Element();
            switchSegment = new DmsSwitchSegment();
        }

        linkNodes.setName(linkInfo.getMark());
        linkNodes.setCode(linkInfo.getMark());
        linkNodes.setEname(linkInfo.getMark());
        linkNodes.setRstype(Long.valueOf(ResTypeEnum.DMS_FA_SWITCH_LINK.getValue()));
        linkNodes.setInstanceclassname(ResTypeEnum.DMS_FA_SWITCH_LINK.getClassName());
        linkNodes.setTopolable(linkInfo.getMark());
        linkNodes.setMark(linkInfo.getMark());
        nodesManager.save(linkNodes);

        //线的属性
        Neport nePortA = this.getNePortFromMap(linkInfo.startPortInfo, idMap);
        Neport nePortZ = this.getNePortFromMap(linkInfo.endPortInfo, idMap);

        switchSegment.setPortaId(nePortA.getId());
        switchSegment.setPortzId(nePortZ.getId());
        switchSegment.setName(nePortA.getName() + "-" + nePortZ.getName());
        switchSegment.setCode(nePortA.getCode() + "-" + nePortZ.getCode());
        switchSegment.setNodeId(linkNodes.getId());
        switchSegmentManager.save(switchSegment);

        //线的拓扑图元素
        element.setGraphyId(graphId);
        element.setElementType(2L);
        element.setNodeId(linkNodes.getId());
        element.setEntityType(Long.valueOf(ResTypeEnum.DMS_FA_SWITCH_LINK.getValue()));
        element.setFromId(this.getElementFromMap(linkInfo.startDevice, idMap).getId());
        element.setToId(this.getElementFromMap(linkInfo.endDevice, idMap).getId());
//        element.setLayerId("575354B61E34752AE0530100007F233A");
        elementManager.save(element);
        elementManager.updateElementLayerAndStyle(element.getId());

        return new String[]{linkInfo.startDevice.getDeviceIP(), linkInfo.endDevice.getDeviceIP()};
    }

    private Element getElementFromMap(DeviceInfo deviceInfo, Map<String, Object> map) {
        String key = this.toMapKey(deviceInfo);
        Element element = (Element) map.get(key);
        if (element == null) {
            Nodes nodes = nodesManager.findByMark(deviceInfo.getMark());
            if (nodes != null) {
                element = elementManager.findByNodeAndGraph(nodes.getId(), graphId);
                if (element != null) {
                    map.put(key, element);
                } else {
                    throw new BusinessException("交换机缺少图元数据");
                }
            } else {
                throw new BusinessException("交换机缺少Nodes数据");
            }
        }
        return element;
    }

    private Neport getNePortFromMap(PortInfo portInfo, Map<String, Object> map) {
        String key = this.toMapKey(portInfo);
        Neport neport = (Neport) map.get(key);
        if (neport == null) {
            neport = neportManager.findByMark(portInfo.getMark());
            if (neport != null) {
                map.put(key, neport);
            } else {
                throw new BusinessException("缺少网管端口与资源端口的映射关系!");
            }
        }
        return neport;
    }

    private String toMapKey(Object obj) {
        if (obj instanceof DeviceInfo) {
            return Element.class + "_" + ((DeviceInfo) obj).getMark();
        } else if (obj instanceof PortInfo) {
            return Neport.class + "_" + ((PortInfo) obj).getMark();
        } else {
            throw new BusinessException(obj.getClass() + "无法识别");
        }
    }

}
*/
